package com.maxlen.coupon.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maxlen.common.utils.*;
import com.maxlen.coupon.dao.SeckillSessionDao;
import com.maxlen.coupon.dao.SeckillSkuRelationDao;
import com.maxlen.coupon.entity.SeckillSessionEntity;
import com.maxlen.coupon.entity.SeckillSkuRelationEntity;
import com.maxlen.coupon.entity.to.SeckillSessionTO;
import com.maxlen.coupon.feign.GoodsFeignService;
import com.maxlen.coupon.service.SeckillSessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    private SeckillSkuRelationDao seckillSkuRelationDao;

    @Autowired
    private GoodsFeignService goodsFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        LambdaQueryWrapper<SeckillSessionEntity> wrapper = new LambdaQueryWrapper<>();
        System.out.println(params);
        if(params.get(Constant.KEY) != null && StringUtils.hasText(params.get(Constant.KEY).toString())){
            wrapper.like(SeckillSessionEntity::getId, params.get(Constant.KEY)).or().like(SeckillSessionEntity::getName, params.get(Constant.KEY));
        }

        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public R getSkuWithLately3Day() {
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        LocalDateTime of = LocalDateTime.of(now, min);
        String startTime = of.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        LocalDate localDate = now.plusDays(3);
        LocalTime max = LocalTime.MAX;
        LocalDateTime localDateTime = LocalDateTime.of(localDate, max);
        String endTime = localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        LambdaQueryWrapper<SeckillSessionEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(SeckillSessionEntity::getStartTime, startTime, endTime);

        List<SeckillSessionEntity> seckillSessionEntityList = baseMapper.selectList(wrapper);
        List<SeckillSessionTO> seckillSessionTOList = new ArrayList<>();
        if(!ObjectUtils.isEmpty(seckillSessionEntityList)){
            List<Long> idList = seckillSessionEntityList.stream().map(SeckillSessionEntity::getId).collect(Collectors.toList());
            List<SeckillSkuRelationEntity> seckillSkuRelationEntityList = seckillSkuRelationDao.getListBatchPromotionSessionId(idList);

            if(!ObjectUtils.isEmpty(seckillSkuRelationEntityList)){
                seckillSessionEntityList.forEach(seckillSessionEntity -> {

                    List<SeckillSessionTO.SeckillSkuRelationTO> seckillSkuRelationTOList = new ArrayList<>();

                    seckillSkuRelationEntityList.forEach(seckillSkuRelationEntity -> {
                        if(seckillSessionEntity.getId().equals(seckillSkuRelationEntity.getPromotionSessionId())){
                            SeckillSessionTO.SeckillSkuRelationTO seckillSkuRelationTO = CopyUtils.copy(seckillSkuRelationEntity, SeckillSessionTO.SeckillSkuRelationTO.class);
                            seckillSkuRelationTOList.add(seckillSkuRelationTO);
                        }
                    });

                    if(!ObjectUtils.isEmpty(seckillSkuRelationTOList)){
                        SeckillSessionTO copy = CopyUtils.copy(seckillSessionEntity, SeckillSessionTO.class);
                        copy.setSeckillSkuRelationTO(seckillSkuRelationTOList);
                        seckillSessionTOList.add(copy);
                    }

                });

                List<Long> collect = new ArrayList<>();

                seckillSessionTOList.forEach(seckillSessionTO -> {
                    List<Long> longList = seckillSessionTO.getSeckillSkuRelationTO().stream().map(SeckillSessionTO.SeckillSkuRelationTO::getSkuId).collect(Collectors.toList());
                    collect.addAll(longList);
                });
                R skuInfoBySkuIdList = goodsFeignService.getSkuInfoBySkuIdList(collect);
                if(!skuInfoBySkuIdList.isOk()) {
                    return R.error();
                }
                List<SeckillSessionTO.SeckillSkuRelationTO.SkuInfoTO> skuInfoTOList = skuInfoBySkuIdList.getData(new TypeReference<List<SeckillSessionTO.SeckillSkuRelationTO.SkuInfoTO>>() {});
                seckillSessionTOList.forEach(seckillSessionTO -> {
                    skuInfoTOList.forEach(skuInfoTO -> {
                        seckillSessionTO.getSeckillSkuRelationTO().forEach(seckillSkuRelationTO -> {
                            if(seckillSkuRelationTO.getSkuId().equals(skuInfoTO.getSkuId())){
                                seckillSkuRelationTO.setSkuInfoTO(skuInfoTO);
                            }
                        });
                    });
                });
            }
        }



        return R.ok().setData(seckillSessionTOList);
    }

}
